﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Transactions;
using Commons.Entity;
using Commons.Entity.CommonEnums;
using Common.Background.Data;
using EF.Commons.Background.Entity;

namespace Common.Background.Service
{
    public partial class SystemServices : ServiceBase
    {
        public SystemServices()
        {
        }

        #region 职位设置

        #region 得到单个职位

        public WebManageRoles GetWebManageRoles(WebManageRoles WebManageRoles)
        {
            var linq = from _data in iWebManageRolesRepository.GetModel()
                       join data2 in iWebManageRole_WebManageMenusRepository.GetModel()
                       on _data.ManageRoleID equals data2.ManageRoleID into list
                       select new WebManageRoles_Ext
                       {
                           WebManageRole_WebManageMenus_Extend = list,
                           ManageRoleID = _data.ManageRoleID,
                           RoleName = _data.RoleName,
                           DepartmentID = _data.DepartmentID,
                           About = _data.About,
                           UpdateDate = _data.UpdateDate,
                           SortNumber = _data.SortNumber,
                           Operator = _data.Operator,
                           Status = _data.Status,
                           OperatorAuthority = _data.OperatorAuthority,
                       };
            return linq.FirstOrDefault(i => i.ManageRoleID.Equals(WebManageRoles.ManageRoleID));
        }

        #endregion 得到单个职位

        #region 得到职位列表

        public PagedList<WebManageRoles> WebManageRolesList(
            WebManageRoles WebManageRoles,
            VPredication vp,
            PagingParam pp)
        {
            var webDepartmentsList = iWebDepartmentsRepository.GetModel();
            var linq = from wmr in iWebManageRolesRepository.GetModel()
                       join wdp in iWebDepartmentsRepository.GetModel() on wmr.DepartmentID equals wdp.DepartmentID
                       where
                           (wmr.RoleName == WebManageRoles.RoleName || WebManageRoles.RoleName == null) &&
                           (wmr.DepartmentID == WebManageRoles.DepartmentID || WebManageRoles.DepartmentID == int.MinValue)
                       select new WebManageRoles_Ext
                       {
                           About = wmr.About,
                           DepartmentID = wmr.DepartmentID,
                           WebDepartmentsList_Extend = webDepartmentsList,
                           ManageRoleID = wmr.ManageRoleID,
                           RoleName = wmr.RoleName,
                           Operator = wmr.Operator,
                           SortNumber = wmr.SortNumber,
                           UpdateDate = wmr.UpdateDate,
                           WebDepartments_Extend = new WebDepartments_Ext()
                           {
                               DepartmentID = wmr.DepartmentID,
                               DepartmentName = wdp.DepartmentName
                           },
                           OperatorAuthority = wmr.OperatorAuthority,
                       };
            return new PagedList<WebManageRoles>(linq.OrderByDescending(item => item.UpdateDate), pp.PageIndex, pp.PageSize);
        }

        #endregion 得到职位列表

        #region 得到所有的职位

        public List<WebManageRoles> WebManageRolesList(WebDepartments WebDepartments)
        {
            var linq = from objRoles in base.iWebManageRolesRepository.GetModel()
                       join objDepartMenent in base.iWebDepartmentsRepository.GetModel()
                       on objRoles.DepartmentID equals objDepartMenent.DepartmentID
                       where WebDepartments.DepartmentID == int.MinValue
                       || objDepartMenent.DepartmentID == WebDepartments.DepartmentID
                       select objRoles;
            return linq.ToList();
        }

        #endregion 得到所有的职位

        #region 修改职位

        public void SetWebManageRoles(WebManageRoles WebManageRoles)
        {
            WebManageRoles.UpdateDate = DateTime.Now.ToShortDateString();
            iWebManageRolesRepository.SetWebManageRoles(WebManageRoles);
        }

        #endregion 修改职位

        #region 添加职位

        public void AddWebManageRoles(WebManageRoles WebManageRoles)
        {
            using (TransactionScope trans = new TransactionScope())
            {
                try
                {
                    WebManageRoles.UpdateDate = DateTime.Now.ToString("yy-MM-dd");

                    WebManageRoles.ManageRoleID = new PKIDTableRepository().GetNewPKID(PKIDTable.WebManageRoles);
                    base.iWebManageRolesRepository.Insert(WebManageRoles);
                    int webManageRolesID = new PKIDTableRepository().GetNewPKID(PKIDTable.WebManageRoles);
                    List<WebManageRole_WebManageMenus> list = new List<WebManageRole_WebManageMenus>();
                    foreach (var i in WebManageRoles.WebManageRole_WebManageMenus_Extend)
                    {
                        WebManageRole_WebManageMenus objWMR_WMM = new WebManageRole_WebManageMenus();
                        objWMR_WMM.ManageRoleID = webManageRolesID;
                        objWMR_WMM.MenuID = i.MenuID;

                        list.Add(objWMR_WMM);
                    }
                    base.iWebManageRole_WebManageMenusRepository.Insert(list);
                    trans.Complete();
                }
                catch (Exception)
                {
                    throw;
                }
                finally
                {
                    trans.Dispose();
                }
            }
        }

        #endregion 添加职位

        #region 删除职位

        public void DeleteWebManageRoles(WebManageRoles WebManageRoles)
        {
            using (TransactionScope trans = new TransactionScope())
            {
                try
                {
                    new WebManageRole_WebManageMenusRepository().DeleteByRoleID(WebManageRoles.ManageRoleID);
                    base.iWebManageRolesRepository.Delete(iWebManageRolesRepository.Find(WebManageRoles.ManageRoleID));
                    trans.Complete();
                }
                catch (Exception)
                {
                    throw;
                }
                finally
                {
                    trans.Dispose();
                }
            }
        }

        #endregion 删除职位
    }

        #endregion 职位设置
}